﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{
    class Class3
    {
        public static void FigureE9(Dictionary<string, Object> data)//ISO 13399-2图片E.9
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层
            //作图
            //参数
            double pi = Math.PI;
            double s1 = 3.175;
            double s = 3;
            double an = 0 * pi / 180;
            double gan = 11 * pi / 180;
            double lbb = 3;
            double w1 = 15;
            double l = 25;
            double le = 20;
            double re = 2;
            string path = Convert.ToString(data["path"]);
            //坐标
            //主视图
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v3 = new Vector2(-lbb, s1 - s);
            Vector2 v2 = new Vector2(v3.X + (v3.Y - v1.Y + lbb * Math.Tan(gan)) * Math.Tan((pi / 2 - gan) / 2) + (v3.Y - v1.Y + lbb * Math.Tan(gan)) * Math.Tan((pi / 2 - gan) / 2) * Math.Sin(gan), v3.Y - (v3.Y - v1.Y + lbb * Math.Tan(gan)) * Math.Tan((pi / 2 - gan) / 2) * Math.Cos(gan));
            Vector2 v4 = new Vector2(-w1, v3.Y);
            Vector2 v5 = new Vector2(v4.X + s * Math.Tan(an), v4.Y - s1);
            Vector2 v6 = new Vector2(-s * Math.Tan(an), v5.Y);
            //俯视图
            Vector2 v7 = new Vector2(v1.X, v6.Y - 20 - re);
            Vector2 v8 = new Vector2(v7.X - re, v7.Y + re);
            Vector2 v9 = new Vector2(v7.X - w1 + re, v8.Y);
            Vector2 v10 = new Vector2(v7.X - w1, v7.Y);
            Vector2 v11 = new Vector2(v10.X, v8.Y - l + re);
            Vector2 v12 = new Vector2(v9.X, v8.Y - l);
            Vector2 v13 = new Vector2(v8.X, v12.Y);
            Vector2 v14 = new Vector2(v7.X, v11.Y);
            Vector2 v15 = new Vector2(v7.X, v13.Y + le);
            Vector2 v16 = new Vector2(v15.X - lbb, v15.Y - lbb);
            Vector2 v17 = new Vector2(v16.X, v13.Y);
            Vector2 v18 = new Vector2(v11.X, v9.Y - le);
            Vector2 v19 = new Vector2(v18.X + lbb, v18.Y + lbb);
            Vector2 v20 = new Vector2(v19.X, v9.Y);
            //线段
            //主视图
            Vector2[] Line1 = { v3, v4 };
            fun.drawBLine(dxf, Line1, 0.3, contour);
            Vector2[] Line2 = { v2, v1 };
            fun.drawBLine(dxf, Line2, 0.3, contour);
            Vector2[] Line3 = { v5, v6 };
            fun.drawBLine(dxf, Line3, 0.3, contour);
            Vector2[] Line4 = { v4, v5 };
            fun.drawBLine(dxf, Line4, 0.3, contour);
            LwPolyline Arc1 = new LwPolyline();
            Arc1.Vertexes.Add(new LwPolylineVertex(v2));
            Arc1.Vertexes.Add(new LwPolylineVertex(v3));
            Arc1.Vertexes[0].Bulge = -Math.Tan((gan + pi / 2) / 4);
            fun.PolyFill(dxf, Arc1, 0.3, contour);
            Vector2[] Line5 = { v1, v6 };
            fun.drawBLine(dxf, Line5, 0.3, contour);
            //俯视图
            Line line6 = new Line(new Vector2(v14.X, v13.Y), new Vector2(v14.X + 10, v13.Y));
            Line line7 = new Line(new Vector2(v14.X, v13.Y), new Vector2(v14.X, v13.Y - 10));
            Arc arc8 = new Arc(new Vector2(v1.X, v1.Y), re, 0, -90);
            Vector2[] Line9 = { v8, v9 };
            fun.drawBLine(dxf, Line9, 0.3, contour);
            Vector2[] Line10 = { v10, v11 };
            fun.drawBLine(dxf, Line10, 0.3, contour);
            Vector2[] Line11 = { v12, v13 };
            fun.drawBLine(dxf, Line11, 0.3, contour);
            Vector2[] Line12 = { v14, v7 };
            fun.drawBLine(dxf, Line12, 0.3, contour);
            Vector2[] Line13 = { v16, v17 };
            fun.drawBLine(dxf, Line13, 0.3, contour);
            Vector2[] Line14 = { v19, v20 };
            fun.drawBLine(dxf, Line14, 0.3, contour);
            LwPolyline Arc15 = new LwPolyline();
            Arc15.Vertexes.Add(new LwPolylineVertex(v7));
            Arc15.Vertexes.Add(new LwPolylineVertex(v8));
            Arc15.Vertexes[0].Bulge = Math.Tan(pi / 8);
            fun.PolyFill(dxf, Arc15, 0.3, contour);
            LwPolyline Arc16 = new LwPolyline();
            Arc16.Vertexes.Add(new LwPolylineVertex(v9));
            Arc16.Vertexes.Add(new LwPolylineVertex(v10));
            Arc16.Vertexes[0].Bulge = Math.Tan(pi / 8);
            fun.PolyFill(dxf, Arc16, 0.3, contour);
            LwPolyline Arc17 = new LwPolyline();
            Arc17.Vertexes.Add(new LwPolylineVertex(v11));
            Arc17.Vertexes.Add(new LwPolylineVertex(v12));
            Arc17.Vertexes[0].Bulge = Math.Tan(pi / 8);
            fun.PolyFill(dxf, Arc17, 0.3, contour);
            LwPolyline Arc18 = new LwPolyline();
            Arc18.Vertexes.Add(new LwPolylineVertex(v13));
            Arc18.Vertexes.Add(new LwPolylineVertex(v14));
            Arc18.Vertexes[0].Bulge = Math.Tan(pi / 8);
            fun.PolyFill(dxf, Arc18, 0.3, contour);
            LwPolyline Arc19 = new LwPolyline();
            Arc19.Vertexes.Add(new LwPolylineVertex(v15));
            Arc19.Vertexes.Add(new LwPolylineVertex(v16));
            Arc19.Vertexes[0].Bulge = Math.Tan(pi / 8);
            fun.PolyFill(dxf, Arc19, 0.3, contour);
            LwPolyline Arc20 = new LwPolyline();
            Arc20.Vertexes.Add(new LwPolylineVertex(v18));
            Arc20.Vertexes.Add(new LwPolylineVertex(v19));
            Arc20.Vertexes[0].Bulge = Math.Tan(pi / 8);
            fun.PolyFill(dxf, Arc20, 0.3, contour);
            //俯视图图层
            line6.Layer = dimensioning;
            line7.Layer = dimensioning;
            //俯视图
            //dxf.AddEntity(line6);
            //dxf.AddEntity(line7);
            //标注样式
            //箭头样式
            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = 0.2;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            //文本样式
            dimensionStyle.TextHeight = 1.0 / 2;
            double offset = 10;
            //线段尺寸标注
            //尺寸2
            LinearDimension dim2 = new LinearDimension(new Line(v1, v6), -v6.X / 2 + offset, 270, dimensionStyle);
            dim2.UserText = "S";
            dxf.AddEntity(dim2);
            //尺寸1
            Angular2LineDimension angdim1 = new Angular2LineDimension(line6, line7, offset, dimensionStyle);
            angdim1.UserText = "EPSR";
            dxf.AddEntity(angdim1);
            //尺寸3
            RadialDimension dimrad4 = new RadialDimension(arc8, -45, offset / 2, dimensionStyle);//DimensionStyle.Default);
            dimrad4.UserText = "RE";
            dxf.AddEntity(dimrad4);
            //尺寸4
            LinearDimension dim4 = new LinearDimension(new Line(v15, v13), (v15.X - v13.X) / 2 + offset, 270, dimensionStyle);
            dim4.UserText = "LE";
            dxf.AddEntity(dim4);

            //尺寸5
            LinearDimension dim5 = new LinearDimension(new Line(v13, v8), re + offset * 2, 270, dimensionStyle);
            dim5.UserText = "L";
            dxf.AddEntity(dim5);
            //尺寸6
            LinearDimension dim6 = new LinearDimension(new Line(v11, v14), -offset, 0, dimensionStyle);
            dim6.UserText = "W1";
            dxf.AddEntity(dim6);
            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
